{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b56e127f",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import torch \n",
    "x_data = [1.0, 2.0, 3.0]\n",
    "y_data = [2.0, 4.0, 6.0]\n",
    "w = torch.tensor([1.0])\n",
    "w.requires_grad = True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "77bdc39a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def forward(x):\n",
    "    return x*w\n",
    "def loss(x,y):\n",
    "    y_pred = forward(x)\n",
    "    return(y_pred - y)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dd58227b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predict(before traning) 4 4.0\n",
      "\tgrad: 1.0 2.0 -2.0\n",
      "\tgrad: 2.0 4.0 -7.840000152587891\n",
      "\tgrad: 3.0 6.0 -16.228801727294922\n",
      "progress: 1 7.315943717956543 1.260688066482544\n",
      "\tgrad: 1.0 2.0 -1.478623867034912\n",
      "\tgrad: 2.0 4.0 -5.796205520629883\n",
      "\tgrad: 3.0 6.0 -11.998146057128906\n",
      "progress: 2 3.9987640380859375 1.4534177780151367\n",
      "\tgrad: 1.0 2.0 -1.0931644439697266\n",
      "\tgrad: 2.0 4.0 -4.285204887390137\n",
      "\tgrad: 3.0 6.0 -8.870372772216797\n",
      "progress: 3 2.1856532096862793 1.5959051847457886\n",
      "\tgrad: 1.0 2.0 -0.8081896305084229\n",
      "\tgrad: 2.0 4.0 -3.1681032180786133\n",
      "\tgrad: 3.0 6.0 -6.557973861694336\n",
      "progress: 4 1.1946394443511963 1.7012479305267334\n",
      "\tgrad: 1.0 2.0 -0.5975041389465332\n",
      "\tgrad: 2.0 4.0 -2.3422164916992188\n",
      "\tgrad: 3.0 6.0 -4.848389625549316\n",
      "progress: 5 0.6529689431190491 1.779128909111023\n",
      "\tgrad: 1.0 2.0 -0.4417421817779541\n",
      "\tgrad: 2.0 4.0 -1.7316293716430664\n",
      "\tgrad: 3.0 6.0 -3.58447265625\n",
      "progress: 6 0.35690122842788696 1.836707353591919\n",
      "\tgrad: 1.0 2.0 -0.3265852928161621\n",
      "\tgrad: 2.0 4.0 -1.2802143096923828\n",
      "\tgrad: 3.0 6.0 -2.650045394897461\n",
      "progress: 7 0.195076122879982 1.8792757987976074\n",
      "\tgrad: 1.0 2.0 -0.24144840240478516\n",
      "\tgrad: 2.0 4.0 -0.9464778900146484\n",
      "\tgrad: 3.0 6.0 -1.9592113494873047\n",
      "progress: 8 0.10662525147199631 1.9107471704483032\n",
      "\tgrad: 1.0 2.0 -0.17850565910339355\n",
      "\tgrad: 2.0 4.0 -0.699742317199707\n",
      "\tgrad: 3.0 6.0 -1.4484672546386719\n",
      "progress: 9 0.0582793727517128 1.9340143203735352\n",
      "\tgrad: 1.0 2.0 -0.1319713592529297\n",
      "\tgrad: 2.0 4.0 -0.5173273086547852\n",
      "\tgrad: 3.0 6.0 -1.070866584777832\n",
      "progress: 10 0.03185431286692619 1.9512161016464233\n",
      "\tgrad: 1.0 2.0 -0.09756779670715332\n",
      "\tgrad: 2.0 4.0 -0.3824653625488281\n",
      "\tgrad: 3.0 6.0 -0.7917022705078125\n",
      "progress: 11 0.017410902306437492 1.9639335870742798\n",
      "\tgrad: 1.0 2.0 -0.07213282585144043\n",
      "\tgrad: 2.0 4.0 -0.2827606201171875\n",
      "\tgrad: 3.0 6.0 -0.5853137969970703\n",
      "progress: 12 0.009516451507806778 1.9733357429504395\n",
      "\tgrad: 1.0 2.0 -0.053328514099121094\n",
      "\tgrad: 2.0 4.0 -0.2090473175048828\n",
      "\tgrad: 3.0 6.0 -0.43272972106933594\n",
      "progress: 13 0.005201528314501047 1.9802868366241455\n",
      "\tgrad: 1.0 2.0 -0.039426326751708984\n",
      "\tgrad: 2.0 4.0 -0.15455150604248047\n",
      "\tgrad: 3.0 6.0 -0.3199195861816406\n",
      "progress: 14 0.0028430151287466288 1.9854258298873901\n",
      "\tgrad: 1.0 2.0 -0.029148340225219727\n",
      "\tgrad: 2.0 4.0 -0.11426162719726562\n",
      "\tgrad: 3.0 6.0 -0.23652076721191406\n",
      "progress: 15 0.0015539465239271522 1.989225149154663\n",
      "\tgrad: 1.0 2.0 -0.021549701690673828\n",
      "\tgrad: 2.0 4.0 -0.08447456359863281\n",
      "\tgrad: 3.0 6.0 -0.17486286163330078\n",
      "progress: 16 0.0008493617060594261 1.9920340776443481\n",
      "\tgrad: 1.0 2.0 -0.01593184471130371\n",
      "\tgrad: 2.0 4.0 -0.062453269958496094\n",
      "\tgrad: 3.0 6.0 -0.12927818298339844\n",
      "progress: 17 0.00046424579340964556 1.9941107034683228\n",
      "\tgrad: 1.0 2.0 -0.011778593063354492\n",
      "\tgrad: 2.0 4.0 -0.046172142028808594\n",
      "\tgrad: 3.0 6.0 -0.09557533264160156\n",
      "progress: 18 0.0002537401160225272 1.9956458806991577\n",
      "\tgrad: 1.0 2.0 -0.00870823860168457\n",
      "\tgrad: 2.0 4.0 -0.03413581848144531\n",
      "\tgrad: 3.0 6.0 -0.07066154479980469\n",
      "progress: 19 0.00013869594840798527 1.9967811107635498\n",
      "\tgrad: 1.0 2.0 -0.006437778472900391\n",
      "\tgrad: 2.0 4.0 -0.025236129760742188\n",
      "\tgrad: 3.0 6.0 -0.052239418029785156\n",
      "progress: 20 7.580435340059921e-05 1.9976202249526978\n",
      "\tgrad: 1.0 2.0 -0.004759550094604492\n",
      "\tgrad: 2.0 4.0 -0.018657684326171875\n",
      "\tgrad: 3.0 6.0 -0.038620948791503906\n",
      "progress: 21 4.143271507928148e-05 1.99824059009552\n",
      "\tgrad: 1.0 2.0 -0.003518819808959961\n",
      "\tgrad: 2.0 4.0 -0.0137939453125\n",
      "\tgrad: 3.0 6.0 -0.028553009033203125\n",
      "progress: 22 2.264650902361609e-05 1.9986991882324219\n",
      "\tgrad: 1.0 2.0 -0.00260162353515625\n",
      "\tgrad: 2.0 4.0 -0.010198593139648438\n",
      "\tgrad: 3.0 6.0 -0.021108627319335938\n",
      "progress: 23 1.2377059647405986e-05 1.9990383386611938\n",
      "\tgrad: 1.0 2.0 -0.0019233226776123047\n",
      "\tgrad: 2.0 4.0 -0.0075397491455078125\n",
      "\tgrad: 3.0 6.0 -0.0156097412109375\n",
      "progress: 24 6.768445018678904e-06 1.9992889165878296\n",
      "\tgrad: 1.0 2.0 -0.0014221668243408203\n",
      "\tgrad: 2.0 4.0 -0.0055751800537109375\n",
      "\tgrad: 3.0 6.0 -0.011541366577148438\n",
      "progress: 25 3.7000872907810844e-06 1.999474287033081\n",
      "\tgrad: 1.0 2.0 -0.0010514259338378906\n",
      "\tgrad: 2.0 4.0 -0.0041217803955078125\n",
      "\tgrad: 3.0 6.0 -0.008531570434570312\n",
      "progress: 26 2.021880391112063e-06 1.9996113777160645\n",
      "\tgrad: 1.0 2.0 -0.0007772445678710938\n",
      "\tgrad: 2.0 4.0 -0.0030469894409179688\n",
      "\tgrad: 3.0 6.0 -0.006305694580078125\n",
      "progress: 27 1.1044940038118511e-06 1.9997127056121826\n",
      "\tgrad: 1.0 2.0 -0.0005745887756347656\n",
      "\tgrad: 2.0 4.0 -0.0022525787353515625\n",
      "\tgrad: 3.0 6.0 -0.0046634674072265625\n",
      "progress: 28 6.041091182851233e-07 1.9997875690460205\n",
      "\tgrad: 1.0 2.0 -0.0004248619079589844\n",
      "\tgrad: 2.0 4.0 -0.0016651153564453125\n",
      "\tgrad: 3.0 6.0 -0.003444671630859375\n",
      "progress: 29 3.296045179013163e-07 1.9998430013656616\n",
      "\tgrad: 1.0 2.0 -0.0003139972686767578\n",
      "\tgrad: 2.0 4.0 -0.0012311935424804688\n",
      "\tgrad: 3.0 6.0 -0.0025491714477539062\n",
      "progress: 30 1.805076408345485e-07 1.9998838901519775\n",
      "\tgrad: 1.0 2.0 -0.00023221969604492188\n",
      "\tgrad: 2.0 4.0 -0.0009107589721679688\n",
      "\tgrad: 3.0 6.0 -0.0018854141235351562\n",
      "progress: 31 9.874406714516226e-08 1.9999140501022339\n",
      "\tgrad: 1.0 2.0 -0.00017189979553222656\n",
      "\tgrad: 2.0 4.0 -0.0006742477416992188\n",
      "\tgrad: 3.0 6.0 -0.00139617919921875\n",
      "progress: 32 5.4147676564753056e-08 1.9999364614486694\n",
      "\tgrad: 1.0 2.0 -0.0001270771026611328\n",
      "\tgrad: 2.0 4.0 -0.0004978179931640625\n",
      "\tgrad: 3.0 6.0 -0.00102996826171875\n",
      "progress: 33 2.9467628337442875e-08 1.999953031539917\n",
      "\tgrad: 1.0 2.0 -9.393692016601562e-05\n",
      "\tgrad: 2.0 4.0 -0.0003681182861328125\n",
      "\tgrad: 3.0 6.0 -0.0007610321044921875\n",
      "progress: 34 1.6088051779661328e-08 1.9999653100967407\n",
      "\tgrad: 1.0 2.0 -6.937980651855469e-05\n",
      "\tgrad: 2.0 4.0 -0.00027179718017578125\n",
      "\tgrad: 3.0 6.0 -0.000560760498046875\n",
      "progress: 35 8.734787115827203e-09 1.9999743700027466\n",
      "\tgrad: 1.0 2.0 -5.125999450683594e-05\n",
      "\tgrad: 2.0 4.0 -0.00020122528076171875\n",
      "\tgrad: 3.0 6.0 -0.0004177093505859375\n",
      "progress: 36 4.8466972657479346e-09 1.9999810457229614\n",
      "\tgrad: 1.0 2.0 -3.790855407714844e-05\n",
      "\tgrad: 2.0 4.0 -0.000148773193359375\n",
      "\tgrad: 3.0 6.0 -0.000308990478515625\n",
      "progress: 37 2.6520865503698587e-09 1.999985933303833\n",
      "\tgrad: 1.0 2.0 -2.8133392333984375e-05\n",
      "\tgrad: 2.0 4.0 -0.000110626220703125\n",
      "\tgrad: 3.0 6.0 -0.0002288818359375\n",
      "progress: 38 1.4551915228366852e-09 1.9999895095825195\n",
      "\tgrad: 1.0 2.0 -2.09808349609375e-05\n",
      "\tgrad: 2.0 4.0 -8.20159912109375e-05\n",
      "\tgrad: 3.0 6.0 -0.00016880035400390625\n",
      "progress: 39 7.914877642178908e-10 1.9999922513961792\n",
      "\tgrad: 1.0 2.0 -1.5497207641601562e-05\n",
      "\tgrad: 2.0 4.0 -6.103515625e-05\n",
      "\tgrad: 3.0 6.0 -0.000125885009765625\n",
      "progress: 40 4.4019543565809727e-10 1.9999942779541016\n",
      "\tgrad: 1.0 2.0 -1.1444091796875e-05\n",
      "\tgrad: 2.0 4.0 -4.482269287109375e-05\n",
      "\tgrad: 3.0 6.0 -9.1552734375e-05\n",
      "progress: 41 2.3283064365386963e-10 1.9999958276748657\n",
      "\tgrad: 1.0 2.0 -8.344650268554688e-06\n",
      "\tgrad: 2.0 4.0 -3.24249267578125e-05\n",
      "\tgrad: 3.0 6.0 -6.580352783203125e-05\n",
      "progress: 42 1.2028067430946976e-10 1.9999970197677612\n",
      "\tgrad: 1.0 2.0 -5.9604644775390625e-06\n",
      "\tgrad: 2.0 4.0 -2.288818359375e-05\n",
      "\tgrad: 3.0 6.0 -4.57763671875e-05\n",
      "progress: 43 5.820766091346741e-11 1.999997854232788\n",
      "\tgrad: 1.0 2.0 -4.291534423828125e-06\n",
      "\tgrad: 2.0 4.0 -1.71661376953125e-05\n",
      "\tgrad: 3.0 6.0 -3.719329833984375e-05\n",
      "progress: 44 3.842615114990622e-11 1.9999983310699463\n",
      "\tgrad: 1.0 2.0 -3.337860107421875e-06\n",
      "\tgrad: 2.0 4.0 -1.33514404296875e-05\n",
      "\tgrad: 3.0 6.0 -2.86102294921875e-05\n",
      "progress: 45 2.2737367544323206e-11 1.999998688697815\n",
      "\tgrad: 1.0 2.0 -2.6226043701171875e-06\n",
      "\tgrad: 2.0 4.0 -1.049041748046875e-05\n",
      "\tgrad: 3.0 6.0 -2.288818359375e-05\n",
      "progress: 46 1.4551915228366852e-11 1.9999990463256836\n",
      "\tgrad: 1.0 2.0 -1.9073486328125e-06\n",
      "\tgrad: 2.0 4.0 -7.62939453125e-06\n",
      "\tgrad: 3.0 6.0 -1.430511474609375e-05\n",
      "progress: 47 5.6843418860808015e-12 1.9999992847442627\n",
      "\tgrad: 1.0 2.0 -1.430511474609375e-06\n",
      "\tgrad: 2.0 4.0 -5.7220458984375e-06\n",
      "\tgrad: 3.0 6.0 -1.1444091796875e-05\n",
      "progress: 48 3.637978807091713e-12 1.9999994039535522\n",
      "\tgrad: 1.0 2.0 -1.1920928955078125e-06\n",
      "\tgrad: 2.0 4.0 -4.76837158203125e-06\n",
      "\tgrad: 3.0 6.0 -1.1444091796875e-05\n",
      "progress: 49 3.637978807091713e-12 1.9999995231628418\n",
      "\tgrad: 1.0 2.0 -9.5367431640625e-07\n",
      "\tgrad: 2.0 4.0 -3.814697265625e-06\n",
      "\tgrad: 3.0 6.0 -8.58306884765625e-06\n",
      "progress: 50 2.0463630789890885e-12 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 51 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 52 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 53 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 54 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 55 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 56 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 57 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 58 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 59 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 60 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 61 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 62 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 63 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 64 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 65 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 66 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 67 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 68 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 69 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 70 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 71 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 72 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 73 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 74 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 75 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 76 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 77 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 78 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 79 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 80 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 81 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 82 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 83 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 84 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 85 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 86 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 87 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 88 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 89 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 90 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 91 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 92 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 93 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 94 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 95 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 96 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 97 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 98 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 99 9.094947017729282e-13 1.9999996423721313\n",
      "\tgrad: 1.0 2.0 -7.152557373046875e-07\n",
      "\tgrad: 2.0 4.0 -2.86102294921875e-06\n",
      "\tgrad: 3.0 6.0 -5.7220458984375e-06\n",
      "progress: 100 9.094947017729282e-13 1.9999996423721313\n",
      "predict(after traning) 4 7.999998569488525\n"
     ]
    }
   ],
   "source": [
    "print(\"predict(before traning)\",4,forward(4).item())\n",
    "lo =[]\n",
    "w1 = []\n",
    "n=[]\n",
    "for epoch in range(100):\n",
    "    for x,y in zip(x_data,y_data):\n",
    "        l = loss(x,y)\n",
    "        l.backward()\n",
    "        print('\\tgrad:',x, y, w.grad.item())\n",
    "        w.data = w.data - 0.01 * w.grad.data\n",
    "        w.grad.data.zero_()\n",
    "    lo.append(l.item())\n",
    "    w1.append(w.item())\n",
    "    n.append(epoch+1)\n",
    "    print(\"progress:\",epoch + 1, l.item(), w.item())\n",
    "print ('predict(after traning)',4, forward(4).item())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2423d3bd",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " 2,\n",
       " 3,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 8,\n",
       " 9,\n",
       " 10,\n",
       " 11,\n",
       " 12,\n",
       " 13,\n",
       " 14,\n",
       " 15,\n",
       " 16,\n",
       " 17,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 21,\n",
       " 22,\n",
       " 23,\n",
       " 24,\n",
       " 25,\n",
       " 26,\n",
       " 27,\n",
       " 28,\n",
       " 29,\n",
       " 30,\n",
       " 31,\n",
       " 32,\n",
       " 33,\n",
       " 34,\n",
       " 35,\n",
       " 36,\n",
       " 37,\n",
       " 38,\n",
       " 39,\n",
       " 40,\n",
       " 41,\n",
       " 42,\n",
       " 43,\n",
       " 44,\n",
       " 45,\n",
       " 46,\n",
       " 47,\n",
       " 48,\n",
       " 49,\n",
       " 50,\n",
       " 51,\n",
       " 52,\n",
       " 53,\n",
       " 54,\n",
       " 55,\n",
       " 56,\n",
       " 57,\n",
       " 58,\n",
       " 59,\n",
       " 60,\n",
       " 61,\n",
       " 62,\n",
       " 63,\n",
       " 64,\n",
       " 65,\n",
       " 66,\n",
       " 67,\n",
       " 68,\n",
       " 69,\n",
       " 70,\n",
       " 71,\n",
       " 72,\n",
       " 73,\n",
       " 74,\n",
       " 75,\n",
       " 76,\n",
       " 77,\n",
       " 78,\n",
       " 79,\n",
       " 80,\n",
       " 81,\n",
       " 82,\n",
       " 83,\n",
       " 84,\n",
       " 85,\n",
       " 86,\n",
       " 87,\n",
       " 88,\n",
       " 89,\n",
       " 90,\n",
       " 91,\n",
       " 92,\n",
       " 93,\n",
       " 94,\n",
       " 95,\n",
       " 96,\n",
       " 97,\n",
       " 98,\n",
       " 99,\n",
       " 100]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "57af55d1",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAD4CAYAAADFAawfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAAsTAAALEwEAmpwYAAAWuklEQVR4nO3de3BV5bnH8e9DCISrXEyREhFQB7VQEQNKexIp1no5bU+vUxlrwVFpz7Qe6zh1ap062naOc4qXSm+Uab3Va2utp7WtHk+hAtaDbhAUQal3Q7EEKvdbSJ7zx7tDEpKQnWSvrDd7/z4za9a+rv2sWcyPN++71nrN3RERkXj1SbsAERE5MgW1iEjkFNQiIpFTUIuIRE5BLSISub5JbPToo4/2cePGJbFpEZGCtHLlyi3uXt7We4kE9bhx48hkMklsWkSkIJnZW+29p64PEZHIKahFRCKnoBYRiVwifdQiIt1VV1dHTU0N+/btS7uUvCorK6OiooLS0tKcv6OgFpEo1dTUMGTIEMaNG4eZpV1OXrg7W7dupaamhvHjx+f8PXV9iEiU9u3bx8iRIwsmpAHMjJEjR3b6rwQFtYhEq5BCulFX9imaoH7pJZg5E+bNS7sSEZG4RNNHvXcvPPUU7NyZdiUiInGJpkU9YEBY792bbh0iIrGJJqjLysJaQS0iMZg/fz4LFiwA4KqrrmLWrFkALF68mIsuuqhHa4kmqNWiFpF2mSWzHEFVVRXLli0DIJPJsGvXLurq6li2bBnV1dU9sdeHRBfUBXZuu4j0UqeffjorV65kx44d9O/fnxkzZpDJZFi2bBlVVVU9Wks0g4lqUYtIu1KYhLu0tJTx48dz11138aEPfYgPfvCDLFmyhFdffZWTTz65R2vpsEVtZhPNbHWzZYeZfT3fhfTvH9YHDkB9fb63LiLSeVVVVdx8881UV1dTVVXF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mb0JPEjo/rgdGGZmjTMLFeIxrwFq3H1F9vnDhOAu5GP9UeANd6919zrgEcLxL/Rj3ai9Y5u3jEszqItm0lwzM+AXwHp3v7XZW78D5mQfzyH0XRcEd7/W3SvcfRzh2C5294uAJcDnsh8rqH0GcPd3gXfMbGL2pbOBdRTwsSZ0eZxpZgOz/9Yb97mgj3Uz7R3b3wFfyp79cSawvVkXSeek3Cl/AbABeA24Lu1BggT3818Ifw69AKzOLhcQ+mz/DPwN+F9gRNq1JrT/M4HHso8nAM8CrwK/BvqnXV8C+zsFyGSP96PA8EI/1sCNwMvAWuCXQP9CPNbAA4R++DrCX0+XtndsCYPnP87m24uEs2K69Lu6hFxEJHIaTBQRiZyCWkQkcgpqEZHIKahFRCKnoBYRiZyCWkQkcgpqEZHI/T8s6osEszKlTQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "x= n\n",
    "y = w1\n",
    "z = lo\n",
    "plt.figure()\n",
    "plt.plot(x,y,color = 'red',linewidth = 2,label = 'w')\n",
    "plt.plot(x,z,color = 'blue',linewidth = 2, label = 'loss')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "219e3db7",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "x_data = [1.0,2.0,3.0]\n",
    "y_data = [2.0,4.0,6.0]\n",
    "w_1 = torch.tensor([0.143])\n",
    "w_1.requires_grad = True\n",
    "w_2 = torch.tensor([1.367])\n",
    "w_2.requires_grad = True\n",
    "b = torch.tensor([0.5626])\n",
    "b.requires_grad = True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "dca5e431",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def forward(x):\n",
    "    return w_1*x**2 + w_2 * x +b\n",
    "def loss(x,y):\n",
    "    y_pre = forward(x)\n",
    "    return (y_pre - y)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "70bcc1b8",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predict(before traning) 4 8.318599700927734\n",
      "predect(after traning) 4 8.33357048034668\n",
      "0.14384876191616058 1.367322325706482 0.5627008080482483 0.0016620417591184378\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAAsTAAALEwEAmpwYAAAVyUlEQVR4nO3df5AU5Z3H8fcnsLipE6PChqRYyK4VohBU0BVDcBF/JKLmNFa8nJzeRaNSZaImXkyCZ0o9L1Yukko8q+A4Tg2aEn9cYnLEkJiUeAX+yrEoqICaPeKP5biwEDV6lAr4vT9m5Cab3Z2ZnZ6Z3Wc/r6oppruf7uf7zA6f7e2e6VZEYGZmQ9976l2AmZllw4FuZpYIB7qZWSIc6GZmiXCgm5klYmS9Oh47dmy0tLTUq3szsyFp3bp1OyKiqbdldQv0lpYWOjo66tW9mdmQJOnFvpYVPeQi6TZJ2yU9U6TdMZL2SDp7IEWamVllSjmGvgyY218DSSOAbwO/zKAmMzMbgKKBHhGrgd8XaXYZ8CNgexZFmZlZ+So+hi5pPHAWcAJwTMUVmZmVYffu3XR1dfHmm2/Wu5RMNTY20tzcTENDQ8nrZHFS9Cbg6xHxjqR+G0qaD8wHmDhxYgZdm9lw19XVxejRo2lpaaFYBg0VEcHOnTvp6uqitbW15PWy+Bx6G3C3pBeAs4HFkj7dR5FLI6ItItqamnr91I2Z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W0adO45ZZbqlZH0UMuEbFaUks/yx8tmHwcaM6gLjOzIeOkk05i9+7d+6aff/75fc9reYnyrE+KXgj0+SFLSfMldUjq6O7uzrhrM7PhLbNAl3QCuUD/el9tImJpRLRFRFtTU1NWXZuZGRl9ykXSEcAtwKkRsTOLbZqZWXkq3kOXNBG4D/jriHi+WHszM6uOonvoku4C5gBjJXUB1wINABGxBLgGGAMszt/hek9f97szM7PqKeVTLvOKLL8IuCiziszMbED81X8zswq98MILTJ06td5lONDNzFLhQDczy8CePXs499xzmTx5MmeffTa7du2qeQ2+OJeZpWN5kWvdDtRfFf+253PPPcett97KrFmz+PznP8/ixYu58sorq1NPH7yHbmaWgQkTJjBr1iwAzjvvPB5++OGa1+A9dDNLRwl70tWiHnfC6DldC95DNzPLwEsvvcRjjz0GwPLlyznuuONqXoMD3cwsA4ceeiiLFi1i8uTJvPLKK1xyySU1r8GHXMzMKtTS0sKzzz5b7zK8h25mlgoHuplZIhzoZmaJcKCbmSXCgW5mlggHuplZIhzoZmYV2n///etdAuBANzNLhgPdzCwjEcFXv/pVpk6dyuGHH84999wDwLZt25g9ezbTpk1j6tSprFmzhr1793L++efva/u9732v4v79TVEzS0a1LogVUdpFv+677z7Wr1/Phg0b2LFjB8cccwyzZ89m+fLlnHLKKVx99dXs3buXXbt2sX79erZu3cozzzwDwKuvvlpxnd5DNzPLyMMPP8y8efMYMWIE48aN4/jjj2ft2rUcc8wxfP/73+e6667j6aefZvTo0RxyyCFs2bKFyy67jF/84hcccMABFfdfNNAl3SZpu6Rn+lguSTdL6pT0lKSjKq7KzGwAIqIqj0rNnj2b1atXM378eM4//3zuuOMODjroIDZs2MCcOXNYsmQJF110UcX9lLKHvgyY28/yU4FJ+cd84J8rrsrMbAhqb2/nnnvuYe/evXR3d7N69WpmzJjBiy++yLhx47j44ou56KKLeOKJJ9ixYwfvvPMOn/nMZ/jmN7/JE088UXH/RY+hR8RqSS39NDkTuCNyv8Yel3SgpA9GxLaKqzMzG0LOOussHnv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Hz+9FdgcEd8tWJT02CU15ffMkfRe4BPkzh88RO4G65DguCPiqohojogWcv+fV0XEuSQ+bkl/Jmn0u8+BTwLPUKX3+aD9pqik08gdc3v3xtQ31Lei6pB0FzCH3OU0fwdcC/wEuBeYSO4Sw5+NiJ4nToc0SccBa4Cn+f9jqn9H7jh6smOXdAS5k2AjyO1Q3RsR10s6hNye68HAk8B5EfFW/Sqtnvwhlysj4lOpjzs/vh/nJ0cCyyPiBkljqML7fNAGupmZlWewHnIxM7MyOdDNzBLhQDczS4QD3cwsEQ50M7NEONDNzBLhQDczS8T/AVhe6+W6Z274AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "print('predict(before traning)',4,forward(4).item())\n",
    "n = []\n",
    "w1 = []\n",
    "w2 = []\n",
    "b_ = []\n",
    "lo = []\n",
    "for epoch in range(50):\n",
    "    for x, y in zip(x_data,y_data):\n",
    "        l = loss(x,y)\n",
    "        l.backward()\n",
    "        w_1.data = w_1.data - 0.00001*w_1.grad.data #梯度下降求w_1\n",
    "        w_1.grad.data.zero_()\n",
    "        w_2.data = w_2.data - 0.00001*w_2.grad.data #求w_2\n",
    "        w_2.grad.data.zero_()\n",
    "        b.data =b.data - 0.00001*b.grad.data #求 b\n",
    "        b.grad.data.zero_()\n",
    "    n.append(epoch+1)\n",
    "    w1.append(w_1.item())\n",
    "    w2.append(w_2.item())\n",
    "    b_.append(b.item())\n",
    "    lo.append(l.item())\n",
    "print('predect(after traning)',4,forward(4).item())\n",
    "print(w_1.item(),w_2.item(),b.item(),l.item())\n",
    "import matplotlib.pyplot as plt\n",
    "plt.figure()\n",
    "plt.plot(n, w1, color = 'red', linewidth = 2 ,label = 'w1')\n",
    "plt.plot(n, w2, color = 'blue', linewidth = 2, label = 'w2')\n",
    "plt.plot(n, b_, color  = 'orange', linewidth = 2, label = 'b')\n",
    "plt.plot(n, lo, color = 'black', linewidth = 2, label = 'loss')\n",
    "plt.legend()\n",
    "plt.show()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "592a47d9",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "x_data = [1.0, 2.0, 3.0]\n",
    "y_data = [2.0, 4.0, 6.0]\n",
    "w_1  = torch.tensor([1.0])\n",
    "w_1.requires_grad = True\n",
    "w_2 = torch.tensor([1.0])\n",
    "w_2.requires_grad = True\n",
    "b = torch.tensor([1.0])\n",
    "b.requires_grad = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ead513b5",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def forward(x):\n",
    "    return w_1*x**2+w_2*x+b\n",
    "def loss(x,y):\n",
    "    y_pred = forward(x)\n",
    "    return(y_pred - y)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f3a283ca",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测结果（before training）： 4 tensor([21.], grad_fn=<AddBackward0>)\n",
      "预测结果（after traning）： 4 tensor([8.6220], grad_fn=<AddBackward0>)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAAsTAAALEwEAmpwYAAAivElEQVR4nO3df3gV5Z338fc3v0ggRAFDoAYLWhZsbaUYWbGGWq1WcNWyLbuitVqr1Fq19Hlq11336nr1aa/uarWttZa6atVWLeuzaK2Lv562V8FdrAYKioKKFNsghASS8Fvy4/v8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65VT8vIZVKLvh5lVAv8JLHT3nb1WryI2HHEy8CPgiTyHd4a7TwdmA18xs1l5bn9AFnvE5IXAYwlWF/r4HcZjfyMPyWuNzexmoBN4OEmVQn0WfgKcAEwDthAbHhmK5tN/bz5vx6+/nJLLz6ASfRJmVkrsH+Rhd1/Se72773T33cHyUqDUzI7JV3zuvjmYbwMeJ/YncrxUHt6ea7OBVe7e1HtFoY9foKlnOCuYb0tQp6DH0cyuAP4GuDRIBH2k8FnICXdvcvcud+8G/j1Ju4U+fiXA3wKLk9XJ1/FLklPy8hlUok8gGNO7D1jn7nckqTMuqIeZzSB2LLfnKb4RZjayZ5nYSbu1vao9CXzeYk4D2uP+RMyXpD2pQh6/OPEPr78c+FWCOs8C55rZqGBo4tygLOfM7DzgG8CF7r43SZ1UPgu5ii/+nM/cJO2+DEw2s0nBX3gXEzvu+fJJYL27NyZama/j109Oyc9nMJdnmo/UCTiD2J9QrwCrg2kOcA1wTVDnOuA1YlcRvAicnsf4jg/aXRPEcHNQHh+fAT8mdsXDq0Bdno/hCGKJ+6i4soIdP2JfOFuADmJjnF8ExgC/Ad4C/h8wOqhbB9wbt+2VwIZg+kIe49tAbGy25zO4KKj7PmBpf5+FPMX38+Cz9QqxhDW+d3zB6znErjJ5O5/xBeUP9Hzm4uoW4vglyyl5+QzqFggiIiGnoRsRkZBTohcRCTklehGRkFOiFxEJOSV6EZGQU6IXEQk5JXoRkZD7/+OZGn6a9zbZAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "print('预测结果（before training）：',4, forward(4))\n",
    "w1 = []\n",
    "w2 = []\n",
    "b_ = []\n",
    "n  =[]\n",
    "lo = []\n",
    "for epoch in range(20):\n",
    "    for x, y in zip(x_data, y_data):\n",
    "        l = loss(x,y)\n",
    "        l.backward()\n",
    "        w_1.data = w_1.data - 0.01*w_1.grad.data\n",
    "        w_1.grad.data.zero_()\n",
    "        w_2.data = w_2.data - 0.01*w_2.grad.data\n",
    "        w_2.grad.data.zero_()\n",
    "        b.data = b.data - 0.01*b.grad.data\n",
    "        b.grad.data.zero_()\n",
    "    w1.append(w_1.item())\n",
    "    w2.append(w_2.item())\n",
    "    b_.append(b.item())\n",
    "    n.append(epoch+1)\n",
    "    lo.append(l.item())\n",
    "print('预测结果（after traning）：', 4, forward(4))\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(n, w1,color = 'red', linewidth = 2, label = 'w1')\n",
    "plt.plot(n, w2,color = 'orange', linewidth = 2, label = 'w2')\n",
    "plt.plot(n, b_,color = 'blue', linewidth = 2, label = 'b')\n",
    "plt.plot(n, lo,color = 'black', linewidth = 2, label = 'loss')\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "17d8df60",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2743476927280426\n",
      "0.8170087337493896\n",
      "0.9644300937652588\n",
      "0.019810274243354797\n"
     ]
    }
   ],
   "source": [
    "print(w_1.item())\n",
    "print(w_2.item())\n",
    "print(b.item())\n",
    "print(l.item())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d4a2e95",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "pytorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}